var StrengthLayer = cc.Layer.extend({
	ctor:function () {
		this._super();

		this.addChild(new SwallowLayer(), 1, SWALLOW_LAYER_TAG);
		
		var menu = ccs.load(res.Strength_json).node;

		this.addChild(menu, 2);

		close = menu.getChildByName("close");
		close.addClickEventListener(this.closeClick.bind(this));
		
		enhenceBar = menu.getChildByName("enhenceBar");
		enhenceBar.addClickEventListener(this.enhenceBar.bind(this));
		
		enhenceBall = ccui.helper.seekWidgetByName(menu,"enhenceBall");
		enhenceBall.addClickEventListener(this.enhenceBall.bind(this));
		
		enhenceCoin = menu.getChildByName("enhenceCoin");
		enhenceCoin.addClickEventListener(this.enhenceCoin.bind(this));
		
		enhenceAd = ccui.helper.seekWidgetByName(menu,"enhenceAd");
		enhenceAd.addClickEventListener(this.enhenceAd.bind(this));
		
		barLevel = ccui.helper.seekWidgetByName(menu,"barLevel");
		coinLevel = ccui.helper.seekWidgetByName(menu,"coinLevel");
		ballLevel = ccui.helper.seekWidgetByName(menu,"ballLevel");
		
		switch(EnhenceSys.barWidthEnhence.get()){
		case 1:
//			enhenceBar.setBright(false);
			enhenceBar.setString(ENHENCE_COIN_2);
			barLevel.loadTexture("res/LV2.png");
			break;
		case 2:
			enhenceBar.setVisible(false);
			barLevel.loadTexture("res/LV3.png");
			break;
		}
		switch(EnhenceSys.disappearCountEnhence.get()){
		case 1:
//			enhenceBall.setBright(false);
			enhenceBall.setString(ENHENCE_COIN_2);
			ballLevel.loadTexture("res/LV2.png");
			break;
		case 2:
			enhenceBall.setVisible(false);
			ballLevel.loadTexture("res/LV3.png");
			break;
		}
		switch(EnhenceSys.coinInternalEnhence.get()){
		case 1:
//			enhenceCoin.setBright(false);
			enhenceCoin.setString(ENHENCE_COIN_2);
			coinLevel.loadTexture("res/LV2.png");
			break;
		case 2:
			enhenceCoin.setVisible(false);
			coinLevel.loadTexture("res/LV3.png");
			break;
		}
		switch(EnhenceSys.adEnhence.get()){
		case 1:
			enhenceAd.setVisible(false);
			break;
		}
		return true;
	},
	closeClick:function(sender){
		
		if(music)
			cc.audioEngine.playEffect(res.buttonClickEffect, false);
		
		barLevel = EnhenceSys.barWidthEnhence.get();
		switch(barLevel) {
		case 1:
			barWidth = 374 * (1 + 1/4);
			break;
		case 2:
			barWidth = 374 * (1 + 1/3);
			break;
		}
		coinInternal = 5 - EnhenceSys.coinInternalEnhence.get();
		disappearCount = 5 - EnhenceSys.disappearCountEnhence.get();
		removeAd = EnhenceSys.adEnhence.get();
		
		this.removeFromParent();
//		this.removeChildByTag(SWALLOW_LAYER_TAG, true);
	},
	enhenceBar: function(sender) {
		if(music)
			cc.audioEngine.playEffect(res.buttonClickEffect, false);
		
		var needCoin;
		if(EnhenceSys.barWidthEnhence.get() == 0)
			needCoin = ENHENCE_COIN_1;
		else
			needCoin = ENHENCE_COIN_2;
		
		if(sys.localStorage.getItem(COIN_COUNT_TAG) >= needCoin){
			coin = sys.localStorage.getItem(COIN_COUNT_TAG) - needCoin;
			sys.localStorage.setItem(COIN_COUNT_TAG, coin);
			coinText.setString(coin);	
			
			EnhenceSys.barWidthEnhence.set();
			switch(EnhenceSys.barWidthEnhence.get()){
			case 1:
//				enhenceBar.setBright(false);
				enhenceBar.setString(ENHENCE_COIN_2);
				barLevel.loadTexture("res/LV2.png");
				break;
			case 2:
				enhenceBar.setVisible(false);
//				enhenceBar.setString("20000");
				barLevel.loadTexture("res/LV3.png");
				break;
			}
			
		}else{
			var toastLayer = new ToastLayer();
			toastLayer.showCoinNotEnough();
			this.addChild(toastLayer, 10);
		}
		
	},
	enhenceBall: function(sender) {
		if(music)
			cc.audioEngine.playEffect(res.buttonClickEffect, false);
		var needCoin;
		if(EnhenceSys.disappearCountEnhence.get() == 0)
			needCoin = ENHENCE_COIN_1;
		else
			needCoin = ENHENCE_COIN_2;
		
		if(sys.localStorage.getItem(COIN_COUNT_TAG) >= needCoin){
			coin = sys.localStorage.getItem(COIN_COUNT_TAG) - needCoin;
			sys.localStorage.setItem(COIN_COUNT_TAG, coin);
			coinText.setString(coin);
			
			EnhenceSys.disappearCountEnhence.set();
			switch(EnhenceSys.disappearCountEnhence.get()){
			case 1:
				enhenceBall.setString(ENHENCE_COIN_2);
				ballLevel.loadTexture("res/LV2.png");
				break;
			case 2:
				enhenceBall.setVisible(false);
				ballLevel.loadTexture("res/LV3.png");
				break;
			}
		}else{
			var toastLayer = new ToastLayer();
			toastLayer.showCoinNotEnough();
			this.addChild(toastLayer, 10);
		}
		
	},
	enhenceCoin: function(sender) {
		if(music)
			cc.audioEngine.playEffect(res.buttonClickEffect, false);
		var needCoin;
		if(EnhenceSys.coinInternalEnhence.get() == 0)
			needCoin = ENHENCE_COIN_1;
		else
			needCoin = ENHENCE_COIN_2;
		
		if(sys.localStorage.getItem(COIN_COUNT_TAG) >= needCoin){
			coin = sys.localStorage.getItem(COIN_COUNT_TAG) - needCoin;
			sys.localStorage.setItem(COIN_COUNT_TAG, coin);
			coinText.setString(coin);	
			
			EnhenceSys.coinInternalEnhence.set();
			switch(EnhenceSys.coinInternalEnhence.get()){
			case 1:
				enhenceCoin.setString(ENHENCE_COIN_2);
				coinLevel.loadTexture("res/LV2.png");
				break;
			case 2:
				enhenceCoin.setVisible(false);
				coinLevel.loadTexture("res/LV3.png");
				break;
			}
		}else{
			var toastLayer = new ToastLayer();
			toastLayer.showCoinNotEnough();
			this.addChild(toastLayer, 10);
		}
		
	},
	enhenceAd: function(sender) {
		if(music)
			cc.audioEngine.playEffect(res.buttonClickEffect, false);
		
		var needCoin = ENHENCE_COIN_AD;
		if(sys.localStorage.getItem(COIN_COUNT_TAG) >= needCoin){
			coin = sys.localStorage.getItem(COIN_COUNT_TAG) - needCoin;
			sys.localStorage.setItem(COIN_COUNT_TAG, coin);
			coinText.setString(coin);	
			
			EnhenceSys.adEnhence.set();
			enhenceAd.setVisible(false);
		}else{
			var toastLayer = new ToastLayer();
			toastLayer.showCoinNotEnough();
			this.addChild(toastLayer, 10);
		}
		
	}
});